﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YellowMap
{
    public class DBHelper
    {

        public static async Task<bool> InsertFavorite(Favorite row)
        {

            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var count = await conn.Table<Favorite>().Where(f => f.PlaceId == row.PlaceId).CountAsync();
            if (count > 0)
            {
                return false;
            }
            try
            {
                await conn.InsertAsync(row);
                return true;
            }
            catch (Exception) { }
            return false;

        }

        public static async Task<bool> CheckFavorite(Detail d)
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var count = await conn.Table<Favorite>().Where(f => f.PlaceId == d.Id).CountAsync();
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static async Task<bool> ClearAllHistory()
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var list = await conn.Table<History>().ToListAsync();
            if (list.Count < 0)
            {
                return false;
            }
            try
            {
                foreach (var item in list)
                {
                    await conn.DeleteAsync(item);
                }
                return true;
            }
            catch (Exception) { }
            return false;
        }

        public static async Task<bool> ClearAllFavourites()
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var list = await conn.Table<Favorite>().ToListAsync();
            if (list.Count < 0)
            {
                return false;
            }
            try
            {
                foreach (var item in list)
                {
                    await conn.DeleteAsync(item);
                }
                return true;
            }
            catch (Exception) { }
            return false;
        }

        public static async Task<bool> DeleteFavouritesItem(Favorite row)
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var count = await conn.Table<History>().Where(f => f.PlaceId == row.PlaceId).CountAsync();
            if (count < 0)
            {
                return false;
            }
            try
            {
                await conn.DeleteAsync(row);
                return true;
            }
            catch (Exception) { }
            return false;
        }

        public static async Task<bool> InsertHistory(History row)
        {

            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var count = await conn.Table<History>().Where(f => f.PlaceId == row.PlaceId).CountAsync();
            if (count > 0)
            {
                return false;
            }
            try
            {
                await conn.InsertAsync(row);
                return true;
            }
            catch (Exception) { }
            return false;

        }

        public static async void InsertDetail(Detail row)
        {
            
                SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
                var n = await conn.Table<Detail>().Where(d => d.Id == row.Id).CountAsync();
                if (n == 0)
                {
                    Insert(row);
                }
          

        }

        public static async void Insert(object row)
        {
            try
            {
                SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
                await conn.InsertAsync(row);
            }
            catch (Exception) { }

        }

        public static async void Remove(object row)
        {
            try
            {
                SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
                await conn.DeleteAsync(row);
            }
            catch (Exception) { }

        }

        public static async Task<Detail> GetDetail(Place p)
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            var list = await conn.Table<Detail>().Where(d => d.Id == p.PlaceId).ToListAsync();
            if (list.Count == 0)
            {
                return null;
            }
            return list.First();
        }


    }
}
